package com.team.est.service.impl;

import com.team.est.dto.BrandDto;
import com.team.est.dto.ClassifyDto;
import com.team.est.dto.ModelDto;
import com.team.est.mapper.BrandMapper;
import com.team.est.mapper.ClassifyMapper;
import com.team.est.mapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/classify/")
public class ClassifyServiceImpl {
    @Autowired(required = false)
    private ClassifyMapper classifyMapper;
    @Autowired(required = false)
    private BrandMapper brandMapper;
    @Autowired(required = false)
    private ModelMapper modelMapper;

    //一服务一方法
    //查询分类树
    @RequestMapping("getClassifyTree")
    public List<ClassifyDto> getClassifyTree() {
        //1.查询所有分类
        List<ClassifyDto> classifyDtos = this.classifyMapper.getAllClassifyDto();
        //2 .循环每个分类：获取当前分类下的品牌，设置到当前分类的品牌属性中
        for (ClassifyDto classifyDto : classifyDtos) {
            //循环一次即为一个分类，通过分类查询品牌集合
            List<BrandDto> brandDtos = brandMapper.getBrandByClassifyId(classifyDto.getId());

            //3.循环每个品牌获取当前品牌下的商品型号，设置到品牌的属性中
            for (BrandDto brandDto : brandDtos) {
                //循环一次即为一个品牌，通过当前品牌的编号查询商品的集合
                List<ModelDto> modelDtos = modelMapper.selectModelByBrandId(brandDto.getId());
                //将商品的集合设置品牌实体的商品属性
                brandDto.setModelList(modelDtos);
            }

            //将品牌集合设置到分类实体
            classifyDto.setBrandList(brandDtos);
        }
        return classifyDtos;
    }
}